package com.huafeng.function;

import com.huafeng.answer.Answer;
import com.huafeng.config.BotPropertiesConfig;
import com.huafeng.config.ScheduledConfig;
import com.huafeng.context.AnswerContext;
import com.huafeng.entity.GroupProperties;
import com.huafeng.utils.MessageUtils;
import com.huafeng.utils.SpringContextUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 花风
 * @date 2023/4/6 12:22
 * @Description 消息处理类
 */
@Slf4j
public class MessageFunctions {
    private static final BotPropertiesConfig botPropertiesConfig = SpringContextUtils.getBean(BotPropertiesConfig.class);

    public static final Map<String, GroupProperties> map = botPropertiesConfig.getManage_groups();

    private static final Integer LIMIT = botPropertiesConfig.getLimit();

    private static final String PREFIX = botPropertiesConfig.getPrefix();

    private static final ExecutorService executors = Executors.newFixedThreadPool(10);
    public static Object sendPrivate(List<String> params){
        executors.submit(() -> {
            //获取发送消息的内容
            String content = params.get(2);
            //获取发送消息的账号
            String userId = params.get(0);

            Integer count = ScheduledConfig.privateCount.get(userId);
            if (count == null){
                MessageUtils.sendMessage("我是晓梦，你可以以" + PREFIX + "开头，来向我提问喔",userId,"private");
                ScheduledConfig.privateCount.put(userId,1);
            }else {
                boolean isLimit = checkLimit(userId, "private", count);
                if (content.startsWith(PREFIX)){
                    //根据回复类型获取对应的回复处理器
                    Answer answer = AnswerContext.get(botPropertiesConfig.getType());
                    //获取回复
                    String message = isLimit ? answer.createAnswer(content.substring(content.indexOf(PREFIX) + 1)) : "今日询问次数已达上限，请明天再来喔";
                    MessageUtils.sendMessage(message,userId,"private");
                    ScheduledConfig.privateCount.put(userId,count + 1);
                }
            }
        });
        return null;
    }

    public static Object sendGroup(List<String> params){
       executors.submit(() -> {
           //获取参数
           String groupId = params.get(0);
           String message = params.get(2);

           if (message.startsWith(PREFIX) && map.containsKey(groupId)){
               //获取群聊已使用次数
               Integer count = ScheduledConfig.groupCount.get(groupId);
               String content = null;
               //根据类型获取群聊的回复处理器
               Answer answer = AnswerContext.get(map.get(groupId).getType());
               if (count == null){
                   content = answer.createAnswer(message.substring(message.indexOf(PREFIX) + 1));
                   ScheduledConfig.groupCount.put(groupId,2);
               }else {
                   boolean isLimit = checkLimit(groupId,"group",count);
                   content = isLimit ? answer.createAnswer(message.substring(message.indexOf(PREFIX) + 1)) : "今日询问次数已达上限，请明天再来喔";
                   //该群聊次数+1
                   ScheduledConfig.groupCount.put(groupId,count + 1);
               }
               MessageUtils.sendMessage(content,groupId,"group");
           }
       });
        return null;
    }

    /**
     * 检查是否超出限制
     * @param id QQ号 或 群号
     * @param type 类型 只有group or private
     * @return 检查结果
     */
    private static boolean checkLimit(String id,String type,Integer count){
        if ("private".equals(type)){
            return LIMIT == -1 || count <= LIMIT;
        }else if ("group".equals(type)){
            Integer limit = map.get(id).getLimit();
            return limit == -1 || count <= limit;
        }else {
            return false;
        }
    }
}
